#include <iostream>
#include <cstdio>
#include <algorithm>

#define N 20001

using namespace std;

int a[N]; int b[N];
int ans = 0;

int main(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        scanf("%d %d", &a[i], &b[i]);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for(int i = 1; i <= n; i++){
        ans += b[i] - a[i];
        if(i < n && b[i] > a[i + 1]){
            ans -= b[i] - a[i + 1];
        }
    }
    cout << ans;
    return 0;
}